<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:kecheng:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kechengList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="课程基本信息" align="center">
        <template slot-scope="scope">
          标题:{{ scope.row.kename }}<br>
          创建时间:{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <el-table-column label="统计信息">
        <template slot-scope="scope">
          预约学员数:{{ scope.row.yunum }}<br>
          评价数:{{ scope.row.pingnum }}
        </template>
      </el-table-column>
      <el-table-column label="价格信息">
        <template slot-scope="scope">
          市场价:{{ scope.row.cprice }}<br>
          会员价:{{ scope.row.hprice }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="yuyue(scope.row)"
          >预约
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="ping(scope.row)"
          >评价
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改期中课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程标题" prop="kename">
          <el-input v-model="form.kename" placeholder="请输入课程标题"/>
        </el-form-item>
        <el-form-item label="市场价" prop="cprice">
          <el-input v-model="form.cprice" placeholder="请输入市场价"/>
        </el-form-item>
        <el-form-item label="会员价" prop="hprice">
          <el-input v-model="form.hprice" placeholder="请输入会员价"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    //预约-->
    <el-dialog title="课程预约" :visible.sync="sp1" width="500px" append-to-body>
      <el-form ref="form" :model="yuyu" :rules="rules" label-width="80px">
        <el-form-item label="姓名">
          <el-select v-model="yuyu.id" clearable placeholder="请选择">
            <el-option
              v-for="item in student"
              :key="item.id"
              :label="item.username"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="yusumit">确 定</el-button>
      </div>
    </el-dialog>

    <!--    评价-->
    <el-dialog title="课程评价" :visible.sync="sp2" width="500px" append-to-body>
      <el-form ref="form" :model="jia" :rules="rules" label-width="80px">
        <el-form-item label="课程名称">
          {{ kename }}
        </el-form-item>
        <el-form-item label="评价内容">
          <el-input v-model="jia.pingJia" placeholder="请输入评价"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="jisumit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listKecheng, getKecheng, delKecheng, addKecheng, updateKecheng, student, yuyu,jia} from "@/api/qizhong/qizhong";

export default {
  name: "Kecheng",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 期中课程表格数据
      kechengList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      yuyu: {},
      sp1: false,
      student: [],
      hprice: 0,
      cprice: 0,
      sp2: false,
      jia: {},
      kename: '',
    };
  },
  created() {
    this.getList();
    this.findStudent()
  },
  methods: {
    /** 查询期中课程列表 */
    getList() {
      this.loading = true;
      listKecheng(this.queryParams).then(response => {
        this.kechengList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        kename: null,
        createTime: null,
        cprice: null,
        hprice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加期中课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKecheng(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改期中课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKecheng(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKecheng(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除期中课程编号为"' + ids + '"的数据项？').then(function () {
        return delKecheng(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/kecheng/export', {
        ...this.queryParams
      }, `kecheng_${new Date().getTime()}.xlsx`)
    },
    //预约
    yuyue(row) {
      this.yuyu={}
      this.yuyu.kId = row.id
      this.sp1 = true
      this.hprice = row.hprice
      this.cprice = row.cprice
    },
    //查找学生
    findStudent() {
      student().then(response => {
        this.student = response.rows
      })
    },
    //确定预约
    yusumit() {
      for (var i = 0; i < this.student.length; i++) {
        if (this.student[i].id == this.yuyu.id) {
          var vip = this.student[i].isVip
          if (vip == 1) {
            alert('是会员')
            var money = this.student[i].yuer

            if (this.hprice > money) {
              this.$message.error('余额不足')
            } else {
              console.log(this.yuyu)
              yuyu({kId: this.yuyu.kId, sId: this.yuyu.id}).then(respon => {
                if (respon=='预约成功'){
                  this.$message.success('预约成功')
                }else {
                  this.$message.error(respon)
                }
                this.getList();
                this.sp1 = false
              })
            }
          } else {
            alert('不是会员')
            var money = this.student[i].yuer
            if (this.cprice > money) {
              this.$message.error('余额不足')
            } else {
              console.log(this.yuyu)
              yuyu({kId: this.yuyu.kId, sId: this.yuyu.id}).then(respon => {
                if (respon=='预约成功'){
                  this.$message.success('预约成功')
                }else {
                  this.$message.error(respon)
                }
                this.getList();
                this.sp1 = false
              })
            }
          }
        }
      }

    },
    ping(row) {
      this.jia={}
      this.jia.kId = row.id
      this.kename = row.kename
      console.log(row)
      this.sp2 = true
    },
    jisumit(){
      jia(this.jia).then(respon => {
        this.$message.success('评价成功')
        this.getList();
        this.sp2 = false
      })
    }
  }
};
</script>
